﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace MUnit
{
    public class Verify
    {
        public static void Fail(string message)
        {
            if (message.Length == 0)
            {
                message = "Failure";
            }

            throw new FailureException(message);
        }

        public static void Assert(bool condition)
        {
            if (!condition)
            {
                throw new FailureException("Assert failed");
            }
        }

        public new static void Equals(object expected, object actual)
        {
            string first = "null";
            string second = "null";

            if (expected != null)
            {
                first = expected.ToString();
            }
            if (actual != null)
            {
                second = actual.ToString();
            }

            if (expected != null)
            {
                if (expected.Equals(actual))
                {
                    return;
                }
            }
            throw new FailureException("Equals fail. Expected: " + first + ", actual: " + second);
        }

        public static void NotEquals(object a, object b)
        {
            string first = "null";
            string second = "null";

            if (a != null)
            {
                first = a.ToString();
            }
            if (b != null)
            {
                second = b.ToString();
            }

            if (a != null)
            {
                if (!a.Equals(b))
                {
                    return;
                }
                throw new FailureException("NotEquals fail. Not Expected: " + first + ", actual: " + second);
            }
        }

        public static void IsNull(object a)
        {
            if (a != null)
            {
                throw new FailureException("IsNull fail. " + a.ToString() + " is not null");
            }
        }

        public static void IsNotNull(object a)
        {
            if (a == null)
            {
                throw new FailureException("IsNotNull fail.");
            }
        }

        private static bool Close(float expected, float actual, float margin)
        {
            return (((expected - margin) < actual) && ((expected + margin) > actual));
        }

        public static void Close(float expected, float actual)
        {
            const float margin = 0.01f;
            if (!Verify.Close(expected, actual, margin))
            {
                throw new FailureException("Close failes. Expected: " + expected.ToString() + " +- " + margin.ToString() + ", Actual: " + actual.ToString());
            }
        }

        public static void Close(PointF expected, PointF actual)
        {
            const float margin = 0.01f;
            if (!Verify.Close(expected.X, actual.X, margin) || !Verify.Close(expected.Y, actual.Y, margin))
            {
                throw new FailureException("Close failes. Expected: " + expected.ToString() + " +- " + margin.ToString() + ", Actual: " + actual.ToString());
            }
        }
    }
}
